/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package robot;
import lejos.nxt.*;
import lejos.pc.comm.*;
import java.io.*;
import javax.swing.*;

/**
 *
 * @author Fredrik
 */
public class BlueT {

    public NXTComm nxtComm;
    
    public void connect() {
        nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
	NXTInfo[] nxtInfo = new NXTInfo[1];
	nxtInfo[0] = new NXTInfo("NXT","00:16:53:06:53:A0");
	System.out.println("Connecting to " + nxtInfo[0].btResourceString);
		
	boolean opened = false;
		
	try {
		opened = nxtComm.open(nxtInfo[0]);
	} catch (NXTCommException e) {
		System.out.println("Exception from open");
	}
		
	if (!opened) {
            System.out.println("Failed to open " + nxtInfo[0].name);
            //System.exit(1);
	}
		
        System.out.println("Connected to " + nxtInfo[0].btResourceString);
    }
    
    public void send(String command) {
	//InputStream is = nxtComm.getInputStream();
        //DataInputStream dis = new DataInputStream(is);
	OutputStream os = nxtComm.getOutputStream();
	DataOutputStream dos = new DataOutputStream(os);

	try {
            System.out.println("Sending..." + command);
            dos.writeChars(command);
            dos.flush();					
	} catch (IOException ioe) {
            System.out.println("IO Exception writing bytes:");
            System.out.println(ioe.getMessage());
	}

    }
    
    
}
